package gov.pbc.nn.kjgl.jfcr.handler;

import gov.pbc.nn.core.bdf.entity.Dept;
import gov.pbc.nn.core.utils.SecurityUtils;
import gov.pbc.nn.kjgl.Constants;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.business.IDept;
import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.uflo.env.Context;
import com.bstek.uflo.model.ProcessInstance;
import com.bstek.uflo.process.handler.AssignmentHandler;
import com.bstek.uflo.process.node.TaskNode;

/**
 * 审核人名字搜索处理人 各个岗位的人，并且返回
 * 
 * @author jack.zhao
 * 
 */
@Component("jfcr.AuditEntryAssignmentHandler")
public class AuditEntryAssignmentHandler implements AssignmentHandler {


	/**
	 * 集成接口的方法主要用于返回流程处理人，根据岗位
	 */
	@Override
	public Collection<String> handle(TaskNode node, ProcessInstance pi, Context context) {
		
		List<String> users = new ArrayList<String>();
		List<String> varUsers = new ArrayList<String>();
		List<IUser> usrsList = new ArrayList<IUser>();
		String kezhang = (String) context.getProcessService().getProcessVariable("kezhangUsername", pi);
		String  chuzhang= (String) context.getProcessService().getProcessVariable("chuzhangUsername", pi);
		//得到机房管理员
		Dept dept = (Dept) ContextHolder.getLoginUser().getDepts().get(0);
//		List<IUser> users = SecurityUtils.getUserByRoleForDeptWithChildren(dept.getCDept().getId(), Constants.ROLE_CZ);
		List<IUser> jgUsers = SecurityUtils.getUserByRoleForDeptWithChildren(dept.getCDept().getId(), Constants.ROLE_JFGLY);
		usrsList.addAll(jgUsers);
		for (IUser u : usrsList) {
			users.add(u.getUsername());
			// 可能会变成cname
			varUsers.add(u.getUsername());
		}
		//添加科长
		users.add(kezhang);
		varUsers.add(kezhang);
		//添加处长
		users.add(chuzhang);
		varUsers.add(chuzhang);
		// 给流程变量添加任务处理人，以后每个人处理之后都从shenhren变量中删除
		context.getProcessService().saveProcessVariable(pi.getId(), "shenheren", varUsers);
		return users;
	}
	
}
